package com.relax;


import com.relax.bean.beanDefinition.TestController;
import com.relax.bean.beanDefinition.impl.TestServiceImpl;
import com.relax.config.MainConfig_beanDefinition;
import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringApplicationDemo_beanDefinition {


    @Test
    public void testIoc() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig_beanDefinition.class);
        //查看容器中所有的bean
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println("============>"+name);
        }

        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(TestController.class);
//        beanDefinition.





        GenericBeanDefinition iTestServiceBeanDefinition = (GenericBeanDefinition) BeanDefinitionBuilder
                .genericBeanDefinition(TestServiceImpl.class).getBeanDefinition();

        System.out.println("注入service的信息是"+iTestServiceBeanDefinition);


        GenericBeanDefinition iTestControllerBeanDefinition = (GenericBeanDefinition) BeanDefinitionBuilder
                .genericBeanDefinition(TestController.class)
                .addConstructorArgReference("testService")
                .addConstructorArgValue("张三")
                .getBeanDefinition();
        System.out.println("注入controller的信息是"+iTestControllerBeanDefinition);


        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
        beanFactory.registerBeanDefinition("testService",iTestServiceBeanDefinition);
        beanFactory.registerBeanDefinition("testController",iTestControllerBeanDefinition);


        System.out.println("========================");
        Object testController = context.getBean("testController");
        System.out.println("取出来的controller是"+testController);
    }



}
